package com.tjbaobao.videoplayol.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.tjbaobao.framework.base.BaseRecyclerAdapter;
import com.tjbaobao.framework.ui.BaseRecyclerView;
import com.tjbaobao.framework.ui.base.BaseLinearLayout;
import com.tjbaobao.framework.utils.DeviceUtil;
import com.tjbaobao.framework.utils.ImageUtil;
import com.tjbaobao.framework.utils.Tools;
import com.tjbaobao.videoplayol.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 *
 * Created by TJbaobao on 2018/1/19.
 */

public class ImgChooseView extends BaseLinearLayout {
    public ImgChooseView(Context context) {
        super(context);
    }

    public ImgChooseView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImgChooseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private MyAdapter mAdapter ;
    private List<ImgChooseInfo> mImgChooseInfoList ;

    private int maxChooseNum ;
    private int chooseNum ;
    private static final int DefItemColor = Color.parseColor("#dfdfdf");

    @Override
    protected void onInitView(Context context, AttributeSet attrs, int defStyleAttr) {
        maxChooseNum = 1;
        chooseNum = 0;
        mImgChooseInfoList = new ArrayList<>();
        mAdapter = new MyAdapter(mImgChooseInfoList,R.layout.img_choose_view_item_layout);
        BaseRecyclerView<Holder, ImgChooseInfo> mRecyclerView = new BaseRecyclerView<>(context);
        mRecyclerView.toGridView(3);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setOnItemClickListener(new MyOnItemClickListener());
        this.addView(mRecyclerView);
        initDefData();
    }

    private void initDefData()
    {
        Bitmap bitmap = Bitmap.createBitmap(200,200, Bitmap.Config.RGB_565);
        bitmap.eraseColor(DefItemColor);
        for (int j = 0; j < 6; j++)
        {
            mImgChooseInfoList.add(new ImgChooseInfo(bitmap));
        }
        mAdapter.notifyDataSetChanged();
    }

    private class MyAdapter extends BaseRecyclerAdapter<Holder,ImgChooseInfo>
    {

        MyAdapter(List<ImgChooseInfo> imgChooseInfos, int itemLayoutRes) {
            super(imgChooseInfos, itemLayoutRes);
        }

        @Override
        public void onBindViewHolder(Holder holder, ImgChooseInfo imgChooseInfo, int position) {
//            float borderWidth = Tools.dpToPx(2);
            if(imgChooseInfo.isSelect())
            {
                holder.mTextView.setText(String.format(Locale.getDefault(),"%d", imgChooseInfo.getPosition()));
                holder.mTextView.setVisibility(VISIBLE);
                holder.mImageView.setBorderColor(context.getResources().getColor(R.color.app_red_off));
            }
            else
            {
                holder.mTextView.setVisibility(INVISIBLE);
                holder.mImageView.setBorderColor(context.getResources().getColor(R.color.app_white));
            }

            Bitmap bitmap =imgChooseInfo.getBitmap();
            int margin = Tools.dpToPx(5);
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();
            int screenWidth = (DeviceUtil.getScreenWidth())/3;

            GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) holder.itemView.getLayoutParams();
            if(layoutParams==null)
            {
                layoutParams = new GridLayoutManager.LayoutParams(screenWidth,screenWidth*height/width);
            }
            layoutParams.width = screenWidth ;
            layoutParams.height = screenWidth*height/width ;
            holder.itemView.setLayoutParams(layoutParams);
            RelativeLayout.LayoutParams layoutParamsImage = (RelativeLayout.LayoutParams) holder.mImageView.getLayoutParams();
            screenWidth = (DeviceUtil.getScreenWidth()-4*margin)/3;
            layoutParamsImage.width = screenWidth ;
            layoutParamsImage.height = screenWidth*height/width ;
            holder.mImageView.setLayoutParams(layoutParamsImage);
            if(bitmap.getWidth()>layoutParams.width&&bitmap.getHeight()>layoutParams.height)
            {
                bitmap = ImageUtil.matrixBitmap(bitmap,layoutParams.width,layoutParams.height);
            }
            holder.mImageView.setImageBitmap(bitmap);
        }

        @Override
        public Holder onGetHolder(View view,int viewType) {
            return new Holder(view);
        }
    }

    @SuppressWarnings({"WeakerAccess", "unused"})
    public class ImgChooseInfo
    {
        public Bitmap mBitmap ;
        public int position = 0;
        public boolean isSelect ;

        public ImgChooseInfo(Bitmap bitmap) {
            mBitmap = bitmap;
        }


        public Bitmap getBitmap() {
            return mBitmap;
        }

        public void setBitmap(Bitmap bitmap) {
            mBitmap = bitmap;
        }

        public int getPosition() {
            return position;
        }

        public void setPosition(int position) {
            this.position = position;
        }

        public boolean isSelect() {
            return isSelect;
        }

        public void setSelect(boolean select) {
            isSelect = select;
        }
    }

    public class Holder extends BaseRecyclerView.BaseViewHolder
    {
        @BindView(R.id.iv_image)
        RoundedImageView mImageView ;

        @BindView(R.id.tv_position)
        TextView mTextView ;

        Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
        @Override
        public void onInitView(View view) {

        }
    }

    private class MyOnItemClickListener implements BaseRecyclerAdapter.OnItemClickListener<Holder,ImgChooseInfo>
    {
        @Override
        public void onItemClick(Holder holder, ImgChooseInfo imgChooseInfo, int i) {
            if(imgChooseInfo.isSelect)
            {
                imgChooseInfo.isSelect = false;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    mImgChooseInfoList.forEach(info -> {
                        if(info.isSelect&&info.position>imgChooseInfo.position)
                        {
                            info.position-=1;
                        }
                    });
                }
                else
                {
                    for(ImgChooseInfo info:mImgChooseInfoList)
                    {
                        if(info.isSelect&&info.position>imgChooseInfo.position)
                        {
                            info.position-=1;
                        }
                    }
                }
                imgChooseInfo.position = 0;
                chooseNum--;
                mAdapter.notifyDataSetChanged();
            }
            else
            {
                if(maxChooseNum==1)
                {
                    for(ImgChooseInfo info:mImgChooseInfoList)
                    {
                        info.isSelect = false;
                        info.position = 0;
                        chooseNum= 0;
                    }
                }
                if(chooseNum<maxChooseNum)
                {
                    imgChooseInfo.isSelect = true;
                    chooseNum++;
                    imgChooseInfo.position = chooseNum;
                    mAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    private boolean isDefData = true;
    public void addInfo(Bitmap bitmap)
    {
        if(isDefData)
        {
            isDefData = false;
            mImgChooseInfoList.clear();
        }
        mImgChooseInfoList.add(new ImgChooseInfo(bitmap));
        mAdapter.notifyDataSetChanged();
    }

    public List<ImgChooseInfo> values()
    {
        return mImgChooseInfoList;
    }
}
